from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, Request, Form
from redis import Redis
from sqlalchemy.orm import Session
from app import schemas
from app.common import get_redis

from app.utils.resp_util import APIResponse
from app.extensions import dependencies as deps
from app.extensions.dependencies import get_query_token2, parse_encryption_header
from app.dependencies.db import get_db
from app.common import exceptions as excs
from app.common.enums.resp_enum import ErrTypes

from app import services
from app.core.parsers import parse_trans_rules

import logging
logger = logging.getLogger(__name__)

router = APIRouter(prefix='/rule', tags=['risk'])

@router.post("/parse_trans_rule", response_model=APIResponse[bool])
#def is_ip_in_white_list(params: dict, r: Redis=Depends(get_redis)):
def is_ip_in_white_list(params: dict):
    logger.info('>> 解析交易风控规则')
    result = parse_trans_rules(params)
    resp = APIResponse()
    resp.data = result
    return resp

@router.get("/refresh_risk_rule_cache", response_model=APIResponse[list])
def refresh_ip_whitelist_cache(db: Session = Depends(get_db), r: Redis=Depends(get_redis)):
    logger.info('>> 刷新规则缓存数据')
    rule_list = services.risk.refresh_risk_rule_cache(db, r)
    resp = APIResponse()
    resp.data = rule_list
    return resp
